<template>
  <!--客户关怀-->
  <div>
    <!--查找-->
    <el-row>
      <span style="padding: 0 10px"><i class="font-14 el-icon-s-open"></i></span>
      <el-select v-model="type" placeholder="全部数据" style="width: 120px" @change="changeType">
        <el-option v-for="item in AllOptions" :key="item.value"
                   :label="item.label" :value="item.value">
        </el-option>
      </el-select>
      <span style="padding: 0 10px 0 30px"><i class="fn el-icon-message-solid"></i></span>
     <!-- <el-input placeholder="请输入内容" style="width: 150px"></el-input>
      <el-button type="text">
        <el-button icon="el-icon-search" size="small" >关怀主题</el-button>
      </el-button>-->
      <el-input @change="changeType" placeholder="请输入内容"  suffix-icon="el-icon-search" style="width: 150px" v-model="search"></el-input>
      <span>关怀主题</span>

    </el-row>
    <!--客户关怀-->
    <el-row style="margin-top: 15px">
      <el-col :span="12">
        <span set-lan="html:客户关怀">客户关怀</span>
        <small class="text-muted font-13">共{{totalSize}}条</small>
      </el-col>
      <el-col :span="12" class="text-right">
        <el-button type="text" @click="dialogFormVisible = true">
          <el-button size="small" icon="el-icon-circle-plus-outline">新建</el-button>
        </el-button>
      </el-col>
      <!--客户关怀dialog-->
      <el-dialog title="客户关怀" :visible.sync="dialogFormVisible"  @open="openDialog" @close='closeDialog' width="60%" style="color: #333333"  >
        <el-form :model="customercare" ref="customercare" label-width="100px" :rules="rules">
          <el-divider><span class="font-14-bold">客户关怀</span></el-divider>
          <el-row>
            <el-col>
              <el-form-item label="关怀主题:">
                <el-input  v-model="customercare.customercareTheme" style="width: 91%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="客户:">
                <el-input :disabled="true" v-model="customercare.customerByCustomerId.customerName" style="width: 91%"></el-input>
              </el-form-item>
              <div class="form-inline" v-show="isCustomer">
                <el-input v-model="customerValue" @input="findCustomer" clearable size="medium" placeholder="拼音.字母.关键字" style="width: 150px"></el-input>
                <el-button @click="findCustomer" size="medium" icon="el-icon-search"></el-button>
                <el-popover placement="top" width="140" v-model="visible">
                  <ul class="cu-ul" v-if="customerList2 != ''">
                    <li @click="choiceCustomer2(item)" v-for="item in customerList2">{{item.customerName}}</li>
                  </ul>
                  <div style="color: #5d9cec;margin: 5px 0;text-align: center" v-if="customerList2 == ''"><i class="el-icon-warning-outline"/>没有该客户！</div>
                </el-popover>
                <el-tooltip class="item" effect="dark" content="浏览选择客户" placement="bottom">
                  <el-button @click="innerVisible(cusCurrPage,cusPageSize)" size="medium" icon="el-icon-s-operation"></el-button>
                </el-tooltip>
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="联系人:">
                <el-select v-model="customercare.customercareContactName" placeholder="未选" style="width: 79%">
                  <el-option v-for="(item,index) in contactList" :key="index" :label="item.contactName" :value="item.contactName">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="日期:">
                <el-date-picker  v-model="customercare.customercareDate"  type="date" style="width: 79%"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="执行人:">
              <el-input :disabled="true" v-model="customercare.employeeByEmpId.empName" style="width: 79%">
                <el-button slot="append" icon="el-icon-search"></el-button>
              </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="类型:">
                <el-select v-model="customercare.customercareType" placeholder="未选" style="width: 79%">
                  <el-option  v-for="(item,index) in types" :key="index"
                              :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item  label="关怀内容:">
                <el-input type="textarea"  v-model="customercare.customercareContent" :autosize="{ minRows: 2, maxRows: 2}" style="width: 79%"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item  label="客户反馈:">
                <el-input type="textarea"  v-model="customercare.customercareFeedback" :autosize="{ minRows: 2, maxRows: 2}" style="width: 79%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="备注:">
                <el-input  v-model="customercare.customercareRemarks" type="textarea" :autosize="{ minRows: 2, maxRows: 3}" style="width: 91%"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12" :offset="7">
              <el-form-item>
                <el-button type="primary" @click="addCustomercare()">保存</el-button>
                <el-button @click="dialogFormVisible = false" >取消</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <!--内层dialog-->
        <el-dialog width="30%" title="全部客户" :visible.sync="innerDialog" append-to-body>
          <!--选择客户-->
          <el-table :data="customerList" style="width: 100%">
            <el-table-column prop="customerId" label="客户编号" >
            </el-table-column>
            <el-table-column prop="customerName" label="客户名" >
            </el-table-column>
            <el-table-column label="请选择" >
              <template slot-scope="scope">
                <el-button size="mini" @click="choiceCustomer(scope.row)" icon="el-icon-check" type="primary" circle style="margin-left: 10px"></el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-row style="margin-top:10px">
            <el-col :span="16">
              <el-pagination layout="prev, pager, next, jumper" :total="cusTotalSize" @current-change="cusChangePage"
                             :current-page.sync="cusCurrPage" :page-size="cusPageSize"></el-pagination>
            </el-col>
          </el-row>
        </el-dialog>
      </el-dialog>
    </el-row>
    <!--客户关怀表格-->
    <el-table ref="multipleTable" :data="customercareList" tooltip-effect="dark"
              style="width: 100%"  @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" ></el-table-column>
      <el-table-column prop="customercareId" label="ID" wsortable></el-table-column>
      <el-table-column prop="customercareTheme" label="关怀主题" ></el-table-column>
      <el-table-column prop="customercareContactName"  label="联系人" ></el-table-column>
      <el-table-column prop="customerByCustomerId.customerName" label="客户" ></el-table-column>
      <el-table-column prop="customercareDate"  label="日期" ></el-table-column>
      <el-table-column prop="customercareType"  label="类型" >
        <template slot-scope="scope">
          <span v-if="scope.row.customercareType===1">分类一</span>
          <span v-if="scope.row.customercareType===2">分类二</span>
          <span v-if="scope.row.customercareType===3">分类三</span>
        </template>
      </el-table-column>
      <el-table-column prop="employeeByEmpId.empName"  label="执行人" ></el-table-column>
      <el-table-column prop="" label="操作" width="135px">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑" placement="bottom">
            <el-button size="mini" @click="handleEdit(scope.row)"
                       icon="el-icon-edit" type="primary" circle style="margin-left: 10px"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
            <el-button size="mini" @click="handleDelete(scope.row)"
                       icon="el-icon-delete" type="danger" circle></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-row style="margin-top:10px">
      <el-col :span="8">
        <el-tooltip class="item" effect="dark" content="删除" placement="bottom">
          <el-button style="padding: 12px 12px" icon="el-icon-delete"
                     @click="handleDeleteMany" type="danger"></el-button>
        </el-tooltip>
      </el-col>
      <el-col :span="16">
        <el-pagination :page-size="pageSize" layout="prev, pager, next, jumper" @current-change="changePage"
                       :total="totalSize"></el-pagination>
      </el-col>
    </el-row>
  </div>
</template>

<script>export default {
  data () {
    return {
      emp: {},
      emp2: {},
      type: '',
      search: '',
      customerList: [], // 员工客户名称
      contactList: [], // 员工联系人集合
      cusPageSize: 2, // 分页页大小
      cusCurrPage: 1, // 当前页码，默认为1
      cusTotalSize: 0, // 总记录数
      isCustomer: true, // 客户搜索显示
      innerDialog: false, // 内层dialog客户
      innerDialogEmp: false, // 内层dialog员工
      customerValue: '', // 模糊查询客户
      customerList2: [], // 模糊查询结果
      visible: false, // 模糊搜索结果
      pageSize: 2, // 分页页大小
      currPage: 1, // 当前页码，默认为1
      totalSize: 0, // 总记录数
      AllOptions: [{
        value: 0,
        label: '全部数据'},
      {
        value: 1,
        label: '分类一'},
      {
        value: 2,
        label: '分类二'},
      {
        value: 3,
        label: '分类三'}],
      dialogFormVisible: false,
      direction: 'rtl',
      customercare: {
        customercareId: 0,
        customercareTheme: '',
        customercareDate: '',
        customercareContent: '',
        customercareType: '',
        customercareFeedback: '',
        customercareRemarks: '',
        customercareContactName: '',
        customerByCustomerId: '',
        employeeByEmpId: ''
      },
      customercareList: [],
      types: [ {
        value: 1,
        label: '分类一'
      }, {
        value: 2,
        label: '分类二'
      }, {
        value: 3,
        label: '分类三'
      }],
      timer: '', // 定义一个定时器的变量
      currentTime: new Date(), // 获取当前时间
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]},
      mulSelection: []
    }
  },
  methods: {
    // 查询当前员工
    loginEmp () {
      this.$axios.get('http://localhost:8088/emp')
        .then(v => {
          this.emp = v.data
          this.emp2 = this.emp
        })
        .catch(function (v) {})
    },
    initData (currPage, pageSize) {
      this.$axios.get('http://localhost:8088/customercare-list', {params: {page: currPage, size: pageSize, search: this.search, type: this.type}})
        .then(v => {
          this.customercareList = v.data.rows
          this.totalSize = v.data.total
        })
        .catch(function (v) {})
    },
    changeType () {
      this.initData(1, this.pageSize, this.search, this.type)
    },
    addCustomercare () {
      this.$refs.customercare.validate((valid) => {
        if (valid) {
          this.$axios.post('http://localhost:8088/customercare-addCustomercare', this.customercare)
            .then(v => {
              // 1、刷新数据
              this.initData(this.currPage, this.pageSize)
              // 2、关闭弹框
              this.dialogFormVisible = false
              // 3、重新初始化表单数据模型
              this.customercare = {
                customercareId: 0,
                customercareTheme: '',
                customercareDate: '',
                customercareContent: '',
                customercareType: '',
                customercareFeedback: '',
                customercareRemarks: '',
                customercareContactName: '',
                customerByCustomerId: '',
                employeeByEmpId: ''
              }
            })
            .catch(function (v) {
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    changePage (val) {
      this.initData(val, this.pageSize, this.search, this.type)
    },
    // 打开dialog事件
    openDialog () {
      this.customercare.employeeByEmpId = this.emp
    },
    // 关闭dialog事件
    closeDialog () {
      this.$refs.customercare.resetFields()
      this.customercare = {
        customercareId: 0,
        customercareTheme: '',
        customercareDate: '',
        customercareContent: '',
        customercareType: '',
        customercareFeedback: '',
        customercareRemarks: '',
        customercareContactName: '',
        customerByCustomerId: '',
        employeeByEmpId: ''
      }
      this.isCustomer = true
      this.emp = this.emp2
    },
    handleEdit (val) {
      this.dialogFormVisible = true
      this.customercare.customercareId = val.customercareId
      this.customercare.customercareTheme = val.customercareTheme
      this.customercare.customercareDate = val.customercareDate
      this.customercare.customercareContent = val.customercareContent
      this.customercare.customercareType = val.customercareType
      this.customercare.customercareFeedback = val.customercareFeedback
      this.customercare.customercareRemarks = val.customercareRemarks
      this.customercare.customercareContactName = val.customercareContactName
      this.customercare.customerByCustomerId = val.customerByCustomerId
      this.customercare.employeeByEmpId = val.employeeByEmpId
      this.emp = val.employeeByEmpId
    },
    openView (val) {
      this.$axios.post('http://localhost:8080/#/qaview')
    },
    // 打开内层dialog
    innerVisible (cusCurrPage, cusPageSize) {
      this.$axios.get('http://localhost:8088/emp-customer-list', {
        params: {
          page: cusCurrPage,
          size: cusPageSize
        }
      })
        .then(v => {
          this.customerList = v.data.rows
          this.cusTotalSize = v.data.total
        })
        .catch(function (v) {})
      this.innerDialog = true
    },
    // 客户分页
    cusChangePage (val) {
      // val就是当前页码
      this.innerVisible(val, this.cusPageSize)
    },
    // 选择客户
    choiceCustomer (val) {
      this.customercare.customerByCustomerId = val
      console.log(this.customercare.customerByCustomerId)
      this.choiceContact(val.customerId)
      this.innerDialog = false
    },
    // 模糊查询客户
    findCustomer () {
      // eslint-disable-next-line eqeqeq
      if (this.customerValue != '') {
        this.visible = true
        this.$axios.get('http://localhost:8088/emp-fuzzy-customer-list', {
          params: {
            customerName: this.customerValue
          }
        })
          .then(v => {
            this.customerList2 = v.data
          })
          .catch(function (v) {})
      } else {
        this.visible = false
      }
    },
    // 模糊选择客户
    choiceCustomer2 (val) {
      console.log(val)
      this.customercare.customerByCustomerId = val
      this.choiceContact(val.customerId)
      this.visible = false
    },
    // 选择联系人
    choiceContact (customerId) {
      this.$axios.get('http://localhost:8088/contact-list', {
        params: {
          customerId: customerId
        }
      })
        .then(v => {
          this.contactList = v.data
        })
        .catch(function (v) {})
    },
    // 选择员工
    choiceEmp (val) {
      this.customercare.employeeByEmpId = val
      console.log(this.customercare.customerByCustomerId)
      this.$axios.get('http://localhost:8088/contact-list', {
        params: {
          customerId: val.customerId
        }
      })
        .then(v => {
          this.contactList = v.data
        })
        .catch(function (v) {})
      this.innerDialog = false
    },
    handleDelete (val) {
      this.$confirm('此操作将永久删除销售机会, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log(val['id'])
        this.$axios.post('http://localhost:8080/save-emp', this.qs.stringify(val['id']))
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleDeleteMany () {
      this.$confirm('此操作将永久删除勾选销售机会, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteAll()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSelectionChange (val) {
      this.mulSelection = val
    },
    deleteAll () {
      this.mulSelection.forEach(row => {
        console.log(row)
        this.$axios.post('http://localhost:8080/#/qaview', this.qs.stringify(row))
      })
    }
  },
  created: function () {
    this.loginEmp()
    this.initData(this.currPage, this.pageSize, this.search, this.type)
  }
}
</script>

<style>
  .font-14{
    font-size: 14px
  }

  .text-muted {
    color: #98a6ad;
  }
  .font-13 {
    font-size: 13px !important;
  }
  .text-right {
    text-align: right;
    float: right;
  }
  .form-inline{
    position: relative;
    top: -15px;
    left: 100px;
  }
  .font-14-bold{
    color: #000000;
    font-weight: bold;
  }
</style>
